define(function (require, exports, module) {
	// require('jquery-easyui-zh');
	// require('jquery-easyui');

	var angular = require('angular');
	require('angular-route');
    require('angular-ui-router');
    var asyncLoader = require('angular-async-loader');

	require('easyui');
	require('form');
	require('security');
 	require('gis');
	require('arcgis3');

    var app = angular.module('app', ['ui.router','ui.easyui','form','security','gis','gis.arcgis'])
    .factory('$app', function($rootScope, $http) {
		$app = {
			// 打开地图查询面板
			attrOpen : false,
			setAttrOpen : function(isOpen){
				if(isOpen==null){
					this.attrOpen = !this.attrOpen;
				}else{
					this.attrOpen = isOpen;
				}
			},
			// 打开查询结果面板
			searchOpen:true,
			setSearchOpen : function(isOpen){
				if(isOpen==null){
					this.searchOpen = !this.searchOpen;
				}else{
					this.searchOpen = isOpen;
				}
			},

			// mis 下zs-content设置高度
			setConHei:function(){
				var hei = $("#zs-main").height() - $("#zs-mis .zs-map-tool").outerHeight();
				$("#zs-mis .zs-content").height(hei);
			}
		};
		for(f in $app){
			$rootScope[f] = $app[f];
		}
		return $app;
	})
    .filter("selected",function(){
		return function(input,classvale){
			if(input==classvale){
				return "selected";
			}else{
				return "";
			}
			
		};
	})
	.controller('appCtrl', ['$rootScope', '$scope', '$app',
		function($rootScope, $scope, $app){
			$scope.initZqMenu=function(){
				var locatorName="zqLoctor";
				$scope.pageObject={};
				$scope.pageObject[locatorName]={};
				var page=$scope.pageObject[locatorName];
				if(page['Zqlst']==null){
					
					page['Zqlst']={};
					var firstZq={pid:'',
							code:'440307',
							display:'龙岗区',
							};
					page['Zqlst'][0]=firstZq;
					$scope.selectZq(firstZq,locatorName,'ZQCODE');
				};
			};
			
			$scope.selectZqlevel=function(r,max,page,field){
				for(var k=0;k<=max;k++){
					var temp=$scope.pageObject[page]['Zqlst'][k];
					if(temp!=null){
						if(r.n==temp.display){
							$scope.selectZq(temp,page,field);
							return;
						}
					}
				}
			};
			
			/**
			 * 定位到位置在渲染
			 * @param  {[type]} code  [description]
			 * @param  {[type]} level [description]
			 * @return {[type]}       [description]
			 */
			$rootScope.dirToLoactor=function(display,level,ijb){
				var queryObectTemp={};
				var layerid=11;//市
				if(ijb==1){
					layerid=11;
					queryObectTemp["SHI:="]=display;
				}else if(ijb==2){//县
					layerid=9;
					queryObectTemp["XIAN:="]=display;
				}else if(ijb==3){//乡
					layerid=6;
					queryObectTemp["XIANG:="]=display;
				}else if(ijb==4){//村
					layerid=2;
					queryObectTemp["CUN:="]=display;
				}
				var temp=config.zqQueryLayer+"/"+layerid;
				var param={url:temp};
				param.infoWindowHeiht="260px";//设置弹窗的高度
				param.ifieldUseZh=false;
				var queryForm="zqQuery";
				$rootScope.initAttSearch(queryForm,param);
				$rootScope.gisObject[queryForm].pageSize=3;
				$rootScope.gisObject[queryForm].keyName="OBJECTID";
				$rootScope.gisObject[queryForm].keyNames=["OBJECTID"];
				$scope.gisObject.zqQuery.attribute=queryObectTemp;
				$rootScope.attSearch("zqQuery",function(sn,results){
					var feature=results.features[0];
					var geo=feature.geometry;
					$scope.addZqGraphic(geo);
					console.log("定位结果是："+results);
				});
			};
			
			$scope.selectZq=function(ZqRow,pageObject,Field){
				var code=ZqRow.code;
				var ijb=0;
				var level=8;
				if(code==null || code==''){
					ijb=0;
				}else if(code.length==2){
					ijb=0;
				}else if(code.length==4){
					ijb=1;
					var level=9;
				}else if(code.length==6){
					ijb=2;
					var level=11;
				}else if(code.length==9){
					ijb=3;
					var level=13;
				}else if(code.length==12){
					ijb=4;
					var level=15;
				}
				var page=$scope.pageObject[pageObject];
				page[Field+"_JB"]=ijb;
				page[Field+"_SID"]=ZqRow.pid;
				var tempCode=ZqRow.code;
				var addZero="";
				if(tempCode!=null && tempCode.length<6){
					for(var k=tempCode.length;k<6;k++)addZero=addZero+"0";
				}
				page[Field]=tempCode+addZero;
				page[Field+'_DESC']=ZqRow.display;
				page['Zqlst'][ijb]=ZqRow;

				if(code.length<9){
					$rootScope.gisFullextent();
					//$rootScope.deDrawAndClearGraphics();
				}else{
					$rootScope.mapAddressLocator(ZqRow.code,level);
					try{
						//$rootScope.dirToLoactor(ZqRow.code,level,ijb);
					}catch(e){
		
					}
					
				}
			};
			
			/**
			 * 添加graphic
			 * geometry 线对象，guid 全局唯一值
			 */
			$scope.addZqGraphic =function(geometry, guid){
				var gh = $rootScope.gisObject.map.graphics;
				if(!gh){
					return null;
				}
				guid = guid ? guid : $rootScope.getGUID();
	
				var outlineSyml = new esri.symbol.SimpleLineSymbol(
				esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([
						$rootScope.getDrawStyle().fillColor.r,
							$rootScope.getDrawStyle().fillColor.g,
							$rootScope.getDrawStyle().fillColor.b,
							$rootScope.getDrawStyle().fillOpacity]),
					$rootScope.getHighLightDefaultStyle().strokeWidth);
	
	
				var fillSymbol = new esri.symbol.SimpleFillSymbol(
					esri.symbol.SimpleFillSymbol.STYLE_SOLID,
					outlineSyml, new dojo.Color([
							0,
							0,
							0,
							0]));
				// 先添加绘制图形
				var graphic = new esri.Graphic(geometry, fillSymbol);
				if(!graphic.attributes){
					graphic.attributes = {};
				}
				graphic.attributes.$guid = guid;
				gh.add(graphic);
				return graphic;
			};
			
			
		}
	]);

    asyncLoader.configure(app);
    module.exports = app;
});